package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.ArtistMapper;
import com.fangyi.yunmusic.mapper.ArtistReviewMapper;
import com.fangyi.yunmusic.mapper.UserIsArtistMapper;
import com.fangyi.yunmusic.mapper.UserMapper;
import com.fangyi.yunmusic.pojo.Artist;
import com.fangyi.yunmusic.pojo.ArtistReview;
import com.fangyi.yunmusic.pojo.User;
import com.fangyi.yunmusic.pojo.UserIsArtist;
import com.fangyi.yunmusic.pojo.front.end.FEArtistReview;
import com.fangyi.yunmusic.pojo.vo.UserVO;
import com.fangyi.yunmusic.service.ArtistReviewService;
import com.fangyi.yunmusic.service.notification.ArtistReviewNotificationService;
import com.fangyi.yunmusic.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
@Service
public class ArtistReviewServiceImpl implements ArtistReviewService {
    @Autowired
    private ArtistReviewMapper artistReviewMapper;
    @Autowired
    private ArtistMapper artistMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserIsArtistMapper userIsArtistMapper;

    @Autowired
    private ArtistReviewNotificationService artistReviewNotificationService;


    @Override
    public void addArtistReview(FEArtistReview feArtistReview) {
        User user=userMapper.selectByPrimaryKey(feArtistReview.getUserid());
        if(user==null){
            throw new YunMusicException("用户不存在！");
        }
        Example example=new Example(UserIsArtist.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userId",feArtistReview.getUserid());
        if(userIsArtistMapper.selectByExample(example).size()==1)
            throw new YunMusicException("用户已经是歌手！");
        ArtistReview artistReview=new ArtistReview();
        BeanUtils.copyProperties(feArtistReview,artistReview);
        artistReview.setNickname(user.getNickname());
        artistReview.setId(Id.nextString());
        artistReview.setSubmitTime(new Date());
        artistReviewMapper.insert(artistReview);
    }

    @Override
    public String deleteArtistReview(String id) {
        ArtistReview artistReview = artistReviewMapper.selectByPrimaryKey(id);
        if(artistReview == null)
            throw new YunMusicException("不存在此审核ID");
        artistReviewMapper.deleteByPrimaryKey(id);

        // 添加到Redis
        artistReviewNotificationService.add(artistReview.getUserid(), null, false);

        return artistReview.getUserid();
    }

    @Override
    public PagedResult selectArtistReview(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArtistReview> artistReviews=artistReviewMapper.selectAll();
        PageInfo<?> pageInfo=new PageInfo<>(artistReviews);
        int pages=pageInfo.getPages();
        long total=pageInfo.getTotal();
        return new PagedResult(artistReviews,page,pages,total);
    }

    @Override
    public ArtistReview selectArtistReviewById(String id) {
        return artistReviewMapper.selectByPrimaryKey(id);
    }

    @Override
    public UserVO allowToBeArtist(String id) {
        ArtistReview artistReview=artistReviewMapper.selectByPrimaryKey(id);;
        if(artistReview==null)
            throw new YunMusicException("歌手审核ID不存在！");
        Artist artist=new Artist();
        BeanUtils.copyProperties(artistReview,artist);
        String artistId=Id.nextString();
        artist.setId(artistId);
        UserIsArtist userIsArtist=new UserIsArtist();
        userIsArtist.setId(Id.nextString());
        userIsArtist.setUserId(artistReview.getUserid());
        userIsArtist.setArtistId(artistId);
        userIsArtistMapper.insert(userIsArtist);
        artistMapper.insert(artist);
        artistReviewMapper.deleteByPrimaryKey(id);

        UserVO userVO = new UserVO();
        userVO.setId(artistReview.getUserid());
        userVO.setArtistId(artistId);

        // 添加到Redis
        artistReviewNotificationService.add(userVO.getId(), artistId, true);

        return userVO;
    }
}
